from pr3da.plugins.Plugin import Pr3daPlugin
from pandac.PandaModules import PNMImage, Filename
import os
import fnmatch

class FileSystemPlugin(Pr3daPlugin):
  def __init__(self, root='.'):
    Pr3daPlugin.__init__(self, 'FileSystemPlugin(%s)' % root)
    self.__root = os.path.abspath(root)
    self.__loader = FileSystemLoader()
  
  def search(self, query):
    print("Calling search on FileSystemPlugin with query='%s'" % query)
    filepaths = []
    for dirpath, dirnames, filenames in os.walk(self.__root):
      filepaths.extend(os.path.join(dirpath, f) for f in fnmatch.filter(filenames, query))
    return filepaths
  
  def getLoader(self):
    return self.__loader


class FileSystemLoader():
  def __init__(self):
    return
  
  def __call__(self, url):
    image = PNMImage()
    url = Filename.fromOsSpecific(url).getFullpath()
    image.read(Filename(url))
    return image